import requests


class TestTpshopLogin:

    # 登录成功
    def test_login_success(self):
        session = requests.Session()
        session.get("http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=verify")
        login_url = "http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=do_login"
        login_header = {"Content-Type": "application/x-www-form-urlencoded"}
        login_body = {"username": "13012345678", "password": "123456", "verify_code": "8888"}
        login_resp = session.post(url=login_url, headers=login_header, data=login_body)
        print("login_resp=", login_resp.json())
        # 断言
        assert 200 == login_resp.status_code
        assert 1 == login_resp.json().get("status")
        assert "登陆成功" == login_resp.json().get("msg")

    def test_username_error(self):
        session = requests.Session()
        session.get("http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=verify")
        login_url = "http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=do_login"
        login_header = {"Content-Type": "application/x-www-form-urlencoded"}
        login_body = {"username": "1301234567811", "password": "123456", "verify_code": "8888"}
        login_resp = session.post(url=login_url, headers=login_header, data=login_body)
        print("login_resp=", login_resp.json())
        # 断言
        assert 200 == login_resp.status_code
        assert -1 == login_resp.json().get("status")
        assert "账号不存在!" == login_resp.json().get("msg")

    def test_password_error(self):
        session = requests.Session()
        session.get("http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=verify")
        login_url = "http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=do_login"
        login_header = {"Content-Type": "application/x-www-form-urlencoded"}
        login_body = {"username": "13012345678", "password": "12345611", "verify_code": "8888"}
        login_resp = session.post(url=login_url, headers=login_header, data=login_body)
        print("login_resp=", login_resp.json())
        # 断言
        assert 200 == login_resp.status_code
        assert -2 == login_resp.json().get("status")
        assert "密码错误!" == login_resp.json().get("msg")
